﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using Library_dll.Entities;
using System.IO;
using System.Timers;
using System.Xml.Serialization;

namespace COMM
{
    public partial class comSatForm : Form
    {
        
        Radar Rad = new Radar();
        Missile MisInfo = new Missile();
        RadarStatus RadInfo = new RadarStatus();
        public comSatForm()
        {
            InitializeComponent();
            //Read the COM_comfig.xml for the model and series
            launchButton.Enabled = false;
            XmlTextReader COM_config = new XmlTextReader("COM_config.xml");
            try
            {
                while (!COM_config.EOF)
                {
                    COM_config.Read();
                    if (!COM_config.IsStartElement("settings")) break;
                    COM_config.Read();
                    modelLabel.Text = COM_config.ReadElementString("model");
                    seriesLabel.Text = COM_config.ReadElementString("series");
                    COM_config.Close();
                }
            }
            catch (Exception ex)
            {
                StreamWriter sw = File.AppendText("Exceptions.txt");
                sw.WriteLine(DateTime.Now + ":" + ex); ;
                sw.Close();
            }
            //Load timer on form startup, continue to call file, have the file be deleted after it reads. After which
            //the Comm stays on teh current XML status.
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void normalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TopMost = false;
        }

        private void alwaysOnTopToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TopMost = true;
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Team 1 Missile Defense Project" + Environment.NewLine + "Communications System by Hui Li", "Communications System", MessageBoxButtons.OK);
        }

        private void toolStripProgressBar1_Click(object sender, EventArgs e)
        {

        }

        private void launchButton_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure?", "Communications System", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                Rad.Firing_Status = 1;
                Rad.HitorMiss1 = Utility.RandomNumber(0, 2);
                //Create the serializer object using the type definition
                XmlSerializer serializer = new XmlSerializer(typeof(Radar));
                TextWriter textWriter = new StreamWriter("../../../../Comm_Serial.xml");
                //Use the serializer object to write out the DS_Obj that holds the form data
                serializer.Serialize(textWriter, Rad);
                textWriter.Close();
                missileStatusLabel.Text = "Firing";
            }
            else
            {
                nameLabel.Hide();
                speedLabel.Hide();
                altitudeLabel.Hide();
                directionLabel.Hide();
                distanceLabel.Hide();
                threatLabel.Hide();
                milesToTargetLabel.Hide();
                missileStatusLabel.Hide();
            }
            launchButton.Enabled = false;
        }

        private void comSatForm_Load(object sender, EventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {

                XmlReader reader = new XmlTextReader("../../../../Radar.Xml");
                XmlSerializer serializer = new XmlSerializer(typeof(Radar));
                Rad = (Radar)serializer.Deserialize(reader);
                
                speedLabel.Text = Convert.ToString(Rad.Speed);
                altitudeLabel.Text = Convert.ToString(Rad.Altitude);
                directionLabel.Text = Convert.ToString(Rad.Direction);
                distanceLabel.Text = Convert.ToString(Rad.Distance);
                threatLabel.Text = Convert.ToString(Rad.Threatlevel);
                nameLabel.Text = Convert.ToString(Rad.TargetName1);
                nameLabel.Show();
                speedLabel.Show();
                altitudeLabel.Show();
                directionLabel.Show();
                distanceLabel.Show();
                threatLabel.Show();
                milesToTargetLabel.Show();
                missileStatusLabel.Show();
                reader.Close();
                Utility.DeleteFile("../../../../Radar.Xml");
                if (Rad.Speed > 1)
                {
                    launchButton.Enabled = true;
                }
            }
            catch (Exception ex)
            {
                //Write ex to Log
            }
            try
            {
                XmlReader reader2 = new XmlTextReader("../../../../MisStatus.xml");
                XmlSerializer serializer2 = new XmlSerializer(typeof(Missile));
                MisInfo = (Missile)serializer2.Deserialize(reader2);
                numberRemainingLabel.Text = Convert.ToString(MisInfo.InventoryTotal1);
                missileModeLabel.Text = "ONLINE";
                reader2.Close(); 
            }
            catch (Exception ex)
            {
                missileModeLabel.Text = "OFFLINE";
            }
            if (MisInfo.Hit1 == 0)
            {
                speedLabel.Text = "0";
                altitudeLabel.Text = "0";
                directionLabel.Text = "0";
                distanceLabel.Text = "0";
                threatLabel.Text = "0";
                nameLabel.Text = "None";
                missileStatusLabel.Text = "Idle";
            }
            else
            {
                launchButton.Enabled = true;
            }
            try
            {
                XmlReader reader3 = new XmlTextReader("../../../../RadStatus.xml");
                XmlSerializer serializer3 = new XmlSerializer(typeof(RadarStatus));
                RadInfo = (RadarStatus)serializer3.Deserialize(reader3);
                numberRemainingLabel.Text = Convert.ToString(MisInfo.InventoryTotal1);
                radarModeLabel.Text = "ONLINE";
                reader3.Close();
            }
            catch (Exception ex)
            {
                radarModeLabel.Text = "OFFLINE";
            }
            Utility.DeleteFile("../../../../MisStatus.Xml");
            Utility.DeleteFile("../../../../RadStatus.Xml");
        }


    }
}
